home *** CD-ROM | disk | FTP | other *** search
- // STreeField.cpp: Implementierung der Klasse STreeField.
- //
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "STreeField.h"
-
- #include <math.h>
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
-
- #define LEFT_OF(a,x,y) a[x-1][y]
- #define RIGHT_OF(a,x,y) a[x+1][y]
- #define UPPER_OF(a,x,y) a[x][y-1]
- #define LOWER_OF(a,x,y) a[x][y+1]
-
- //////////////////////////////////////////////////////////////////////
- // Konstruktion/Destruktion
- //////////////////////////////////////////////////////////////////////
-
- STreeField::STreeField()
- {
- dragging = -1;
- lastPos[0] = 0;
- lastPos[1] = 0;
- }
-
- STreeField::~STreeField()
- {
-
- }
-
- void STreeField::create(STexture *baum, STexture *stamm,SSoundSystem* sound)
- {
- soundSystem = sound;
-
- for (int k = 0; k < MATRIX_SIZE; k += 1)
- {
- for (int j = 0; j < MATRIX_SIZE; j += 1)
- {
- matrix[k][j] = false;
- }
- }
-
- //Mittelpunkt
- // matrix[3][3] = false;
-
- int i = 0;
- int* matrixPosition;
-
- for (float x = -3.0f*SPACE; x <= 3.0f*SPACE; x += SPACE)
- {
- for (float y = -3.0f*SPACE; y <= 3.0f*SPACE; y += SPACE)
- {
- if ( (x == -SPACE) || (x == SPACE) || (x == 0.0f) ||
- (y == -SPACE) || (y == SPACE) || (y == 0.0f))
- {
- if (!((x == 0.0f) && (y == 0.0f)))
- {
- trees[i].create(baum,stamm);
- matrixPosition = trees[i].resolveFinalPos(x,y);
- trees[i].setFinalPos(matrixPosition[0],matrixPosition[1]);
- //trees[i].setFinalPos(matrixPosition[0],matrixPosition[1]);
-
-
- matrix[ matrixPosition[0] ][ matrixPosition[1] ] = true;
-
-
- i += 1;
-
- if (i >= NUM_TREES)
- {
- return;
- }
- }
- }
- }
- }
-
- }
-
- void STreeField::update(float frametime)
- {
- for (int i = 0; i < NUM_TREES; i += 1)
- {
- trees[i].update(frametime);
- }
- }
-
- bool STreeField::click(float x, float y)
- {
- for (int i = 0; i < NUM_TREES; i += 1)
- {
- if (trees[i].click(x,y))
- {
- lastPos[0] = trees[i].getXMatrixPos();
- lastPos[1] = trees[i].getYMatrixPos();
-
- dragging = i;
- return true;
- }
- }
-
- return false;
- }
-
- void STreeField::move(float x, float y)
- {
- if (dragging != -1)
- {
- trees[dragging].setPosition(x,y,0.0f);
- }
- }
-
- void STreeField::release(float x, float y)
- {
- if (dragging == -1)
- {
- return;
- }
-
- int* possibility = trees[dragging].resolveFinalPos(x,y);
-
-
- //TRACE2("RESOLVED: %d,%d\n",possibility[0],possibility[1]);
-
-
- if (possibility[0] != -1 && possibility[1] != -1 &&
- !matrix[ possibility[0] ][ possibility[1] ])
- {
- //TESTEN OB FUNKTIONIERT
- // + ⁿbersprungenen Baum zerst÷ren
-
- int xLength = lastPos[0]-possibility[0];
- int yLength = lastPos[1]-possibility[1];
-
- //*****************
- // TESTs
- // 1.Der Baum muss zwei schritte bewegt worden sein
- // und nicht diagonal xLength=yLength
-
- if ((fabs(xLength) == 2 && fabs(xLength) < 3) ||
- (fabs(yLength) == 2 && fabs(yLength) < 3) &&
- (fabs(xLength) != fabs(yLength)))
- {
- //Mittelpunkt finden (Baum dazwischen)
-
- int xCenter = (lastPos[0]+possibility[0])/2;
- int yCenter = (lastPos[1]+possibility[1])/2;
-
- if (matrix[xCenter][yCenter] == true)
- {
- matrix[lastPos[0]][lastPos[1]] = false;
- matrix[possibility[0]][possibility[1]] = true;
- matrix[xCenter][yCenter] = false;
-
- trees[dragging].setFinalPos(possibility[0],possibility[1]);
- trees[dragging].setState(STATE_SHOWN);
-
-
- for (int i = 0; i < NUM_TREES; i += 1)
- {
- if (trees[i].getXMatrixPos() == xCenter &&
- trees[i].getYMatrixPos() == yCenter &&
- trees[i].getState() != STATE_HIDDEN &&
- trees[i].getState() != STATE_DESTROYING)
- {
- trees[i].setState(STATE_DESTROYING);
- break;
- }
- }
-
-
- //***********************************************
-
- dragging = -1;
-
- //***SOUND***
-
- soundSystem->playSample(SAMPLE_RELEASE);
-
- return;
- }
-
- }
- }
-
-
- trees[dragging].setFinalPos(lastPos[0],lastPos[1]);
- trees[dragging].setState(STATE_SHOWN);
-
- dragging = -1;
- }
-
- bool STreeField::isDone()
- {
- for (int i = 0; i < MATRIX_SIZE; i += 1)
- {
- for (int j = 0; j < MATRIX_SIZE; j += 1)
- {
- if (matrix[i][j] /*&&
- !((i < 2 && j < 2) ||
- (i > 4 && j < 2) ||
- (i < 2 && j > 4) ||
- (i > 4 && j > 4) ) */)
- {
- //LINKS
- if (i != 0 && i != MATRIX_SIZE-1)
- {
- if (j < 2 || j > 4)
- {
- if ( i+1 < 5)
- {
- if (LEFT_OF(matrix,i,j) && !RIGHT_OF(matrix,i,j))
- {
- return false;
- }
- }
- }
- else
- {
- if (LEFT_OF(matrix,i,j) && !RIGHT_OF(matrix,i,j))
- {
- return false;
- }
- }
-
- }
-
- //RECHTS
- if (i != 0 && i != MATRIX_SIZE-1)
- {
- if (j < 2 || j > 4)
- {
- if (i-1 >= 2)
- {
- if (RIGHT_OF(matrix,i,j) && !LEFT_OF(matrix,i,j))
- {
- return false;
- }
- }
- }
- else
- {
- if (RIGHT_OF(matrix,i,j) && !LEFT_OF(matrix,i,j))
- {
- return false;
- }
- }
- }
-
- //OBEN
- if (j != 0 && j != MATRIX_SIZE-1)
- {
- if (i < 2 || i > 4)
- {
- if (j+1 < 5)
- {
- if (UPPER_OF(matrix,i,j) && !LOWER_OF(matrix,i,j))
- {
- return false;
- }
- }
- }
- else
- {
- if (UPPER_OF(matrix,i,j) && !LOWER_OF(matrix,i,j))
- {
- return false;
- }
- }
- }
-
- //UNTEN
- if (j != 0 && j != MATRIX_SIZE-1)
- {
- if (i < 2 || i > 4)
- {
- if (j-1 >= 2)
- {
- if (LOWER_OF(matrix,i,j) && !UPPER_OF(matrix,i,j))
- {
- return false;
- }
- }
-
- }
- }
- }
- }
- }
-
- return true;
- }
-
- int STreeField::treesLeft()
- {
- int result = 0;
-
- for (int i = 0; i < MATRIX_SIZE; i += 1)
- {
- for (int j = 0; j < MATRIX_SIZE; j += 1)
- {
- if (matrix[i][j])
- {
- result++;
- }
- }
- }
-
- return result;
- }
-